import { CoolController, BaseController } from '@cool-midway/core';
import { IotCardCardEntity } from '../../entity/card';
import { IotCardCardService } from '../../service/card';
import { Body, Inject, Post } from '@midwayjs/core';
import { DeviceDeviceEntity } from '../../../device/entity/device';

/**
 * 物联网卡信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: IotCardCardEntity,
  service: IotCardCardService,
  pageQueryOp: {
    fieldEq: ['a.iccid', 'a.status'],
    select: ['a.*', 'b.name as deviceName'],
    join: [
      {
        entity: DeviceDeviceEntity,
        alias: 'b',
        condition: 'a.deviceId = b.id',
        type: 'leftJoin',
      },
    ],
  },
})
export class AdminIotCardCardController extends BaseController {
  @Inject()
  iotCardCardService: IotCardCardService;

  /**
   * 充值流量接口
   * @param iccid - 物联网卡的 ICCID
   * @param traffic - 需要充值的流量（单位 MB）
   */
  @Post('/rechargeTraffic', { summary: '充值流量接口' })
  async rechargeTraffic(
    @Body('iccid') iccid: string,
    @Body('traffic') traffic: number
  ) {
    try {
      await this.iotCardCardService.rechargeTraffic(iccid, traffic);
      return {
        success: true,
        message: '充值成功',
      };
    } catch (error) {
      return {
        success: false,
        message: error.message,
      };
    }
  }

  /**
   * 充值短信数量接口
   * @param iccid - 物联网卡的 ICCID
   * @param count - 需要充值的短信数量
   */
  @Post('/rechargeSMS', { summary: '充值短信数量接口' })
  async rechargeSMS(
    @Body('iccid') iccid: string,
    @Body('traffic') count: number
  ) {
    try {
      await this.iotCardCardService.rechargeSMS(iccid, count);
      return {
        success: true,
        message: '充值成功',
      };
    } catch (error) {
      return {
        success: false,
        message: error.message,
      };
    }
  }
}
